﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rem.Objects;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

namespace FileDB
{
    public partial class AdditionInfo
    {
        public int Id { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }

        public static string RootDir = "AdditionInfos";
        public static string FullRootDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
            DataManager.DataDir, RootDir);


        public static List<AdditionInfo> GetAdditionalInfo(int contractId)
        {
            List<AdditionInfo> infos = new List<AdditionInfo>();
            if (contractId < 1)
                return infos;
            string fileName = GetFileName(contractId);
            if (!File.Exists(fileName))
            {
                return infos;
            }
            XDocument xml = XDocument.Load(fileName);
            var root = xml.Root;
            foreach (var e in root.Elements())
            {
                infos.Add(new AdditionInfo()
                {
                    Id = Int32.Parse( e.Attribute("ID").Value),
                    Key = e.Attribute("key").Value,
                    Value = e.Attribute("value").Value
                });
            }
            return infos;

        }

        public static bool DeleteAdditionalInfo(int contractId, AdditionInfo info)
        {
            //List<AdditionInfo> infos = new List<AdditionInfo>();
            if (contractId < 1)
                return false;
            string fileName = GetFileName(contractId);
            if (!File.Exists(fileName))
            {
                return false;
            }
            XDocument xml = XDocument.Load(fileName);
            var root = xml.Root;
            AdditionInfo ainfo = null;
            XElement element = null;
            foreach (var e in root.Elements())
            {
                ainfo = new AdditionInfo()
                {
                    Id = Int32.Parse(e.Attribute("ID").Value),
                    Key = e.Attribute("key").Value,
                    Value = e.Attribute("value").Value
                };
                if(ainfo.Id == info.Id)
                {
                    element = e;
                    break;
                }
            }

            if (element != null)
            {
                element.Remove();
            }
            xml.Save(fileName);
            return true;
            //return GetAdditionalInfo(contractId);
        }
        public static bool UpdateAdditionalInfo(int contractId, AdditionInfo info)
        {
            //List<AdditionInfo> infos = new List<AdditionInfo>();
            if (contractId < 1)
                return false;
            string fileName = GetFileName(contractId);
            if (!File.Exists(fileName))
            {
                return false;
            }
            XDocument xml = XDocument.Load(fileName);
            var root = xml.Root;
            AdditionInfo ainfo = null;
            XElement element = null;
            foreach (var e in root.Elements())
            {
                ainfo = new AdditionInfo()
                {
                    Id = Int32.Parse(e.Attribute("ID").Value),
                    Key = e.Attribute("key").Value,
                    Value = e.Attribute("value").Value
                };
                if (ainfo.Id == info.Id)
                {
                    element = e;
                    break;
                }
            }

            if (element != null)
            {
                element.Attribute("key").SetValue(info.Key);
                element.Attribute("value").SetValue(info.Value);
            }

            xml.Save(fileName);
            return true;
            //return GetAdditionalInfo(contractId);
        }

        public static List<AdditionInfo> SaveAdditionalInfo(int contractId, AdditionInfo info)
        {
            List<AdditionInfo> infos = new List<AdditionInfo>();
            if (contractId < 1)
                return infos;
            string fileName = GetFileName(contractId);
            if (!File.Exists(fileName))
            {
                XElement r = new XElement("AddtionalInfos", new XAttribute(QueryConstants.MaxId, 0.ToString("d8")));
                r.Save(fileName);
            }
            XDocument xml = XDocument.Load(fileName);
            var root = xml.Root;
            int nextId = Int32.Parse(root.Attribute(QueryConstants.MaxId).Value) + 1;
            root.Add(new XElement("AddtionalInfo",
                new XAttribute("ID", nextId.ToString("d8")),
                new XAttribute("key", info.Key),
                new XAttribute("value", info.Value)));
            xml.Root.Attribute(QueryConstants.MaxId).SetValue(nextId.ToString("d8"));
            xml.Save(fileName);
            return GetAdditionalInfo(contractId);
        }

        public static string GetFileName(int Id)
        {
            return Path.Combine(FullRootDir,
                string.Format("{0}.xml",
                Id.ToString("d8")));
        }

        internal static void CreateDefaultTemplate()
        {

        }
    }
}
